package chapter8;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

/**
 * 使用Java8新增的forEach配合Lambda表达式遍历集合元素
 */
public class CollectionEach {
    public static void main(String[] args) {
        //使用lambda表达式遍历集合
        Collection books = new HashSet();
        books.add("轻量级JavaEE企业应用实战");
        books.add("疯狂Java讲义");
        books.add("疯狂Android讲义");
        books.forEach((book)-> System.out.println(book));

        //使用Iterator接口遍历结合
        Iterator it = books.iterator();
        while (it.hasNext()) {
            String book = (String) it.next();
            System.out.println(book);
            if(book.equals("疯狂Java讲义")) {
                it.remove();
            }
            //对迭代变量赋值，不影响集合中的元素
            book = "测试字符串";
        }
        System.out.println(books);
    }
}
